package UI;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


//本类即是ActionListener的实现类
public class LoginJFrame extends JFrame implements ActionListener {
    //登录窗口
    //创建一个登录按钮对象
    JButton login = new JButton(new ImageIcon("image\\login.jpg"));
    //创建一个注册按钮对象
    JButton register = new JButton(new ImageIcon("image\\register.jpg"));
    //创建一个用户框
    JLabel userFrame = new JLabel(new ImageIcon("image\\UserName.jpg"));
    //创建一个密码框
    JLabel passwordFrame = new JLabel(new ImageIcon("image\\Password.jpg"));
    //创建验证码框
    JLabel captchaFrame = new JLabel(new ImageIcon("image\\验证码.jpg"));
    //验证码
    String code = RandomCaptcha.Code();
    //创建刷新按钮框
    JButton refresh = new JButton(new ImageIcon("image\\refresh.jpg"));
    //创建三个文本框
    JTextField userField = new JTextField();
    JTextField passwordField = new JTextField();
    JTextField captchaField = new JTextField();
    //定义边框样式
    MatteBorder border = new MatteBorder(2,0,2,2,new Color(0,0,0));
    public LoginJFrame() {
        //初始化/加载界面
        initFrame();
        initBackGround();
    }
    private void initFrame() {

        //login.setContentAreaFilled(true);//这个方法本来是用于设置无色按钮的，现在用不上
        //设置按钮参数
        login.setBounds(85, 320, 200, 80);
        login.addActionListener(this);

        register.setBounds(365, 320, 200, 80);
        register.addActionListener(this);

        refresh.setBounds(525, 230,45,45);
        refresh.addActionListener(this);
        //框
        userFrame.setBounds(85, 60, 480, 60);
        passwordFrame.setBounds(85, 140, 480, 60);
        captchaFrame.setBounds(85, 232, 183, 45);
        //创建验证码框
        JLabel captchaCode = new JLabel(code);
        captchaCode.setFont(new Font("微雅软黑", Font.BOLD, 24));//setFont:设置字体样式和大小
        captchaCode.setBounds(180, 235, 100, 40);
        //文本域
        userField.setBounds(150, 60, 420, 60);
        userField.setFont(new Font("微雅软黑", Font.BOLD, 28));
        userField.setBorder(border);
        passwordField.setBounds(150, 140, 420, 60);
        passwordField.setFont(new Font("微雅软黑", Font.BOLD, 28));
        passwordField.setBorder(border);
        captchaField.setBounds(270, 230, 250, 45);
        captchaField.setFont(new Font("微雅软黑", Font.BOLD, 24));
        captchaField.setBorder(border);
        //添加
        this.getContentPane().add(userField);
        this.getContentPane().add(passwordField);
        this.getContentPane().add(login);
        this.getContentPane().add(register);
        this.getContentPane().add(refresh);
        this.getContentPane().add(userFrame);
        this.getContentPane().add(passwordFrame);
        this.getContentPane().add(captchaCode);
        this.getContentPane().add(captchaFrame);
        this.getContentPane().add(captchaField);
        //登录窗口
        this.setTitle("拼图游戏——登录");
        this.setSize(650, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setVisible(true);
    }
    private void initBackGround() {
        JLabel jLabel = new JLabel(new ImageIcon("image\\LoginBackground.png"));
        jLabel.setBounds(-10, -15, 650, 500);
        this.getContentPane().add(jLabel);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //事件监听
        //根据事件执行对应程序
        if (e.getSource() == login) {
            if (compare("password.txt", passwordField.getText())) {
                if (compare("Users.txt", userField.getText())) {
                    if (code.equalsIgnoreCase(captchaField.getText())) {
                        new GameJFrame();
                    } else {
                        warning("验证码错误");
                    }
                } else {
                    warning("登录失败，密码错误");
                }
            } else {
                warning("登录失败，用户不存在");
            }
        } else if (e.getSource() == register) {
            new RegisterJFrame();
        } else if (e.getSource() == refresh) {
            code = RandomCaptcha.Code();
            this.getContentPane().removeAll();
            initFrame();
            initBackGround();
            this.getContentPane().repaint();
        }
    }
    //弹窗警告
    private static void warning(String text) {
        JDialog jDialog = new JDialog();
        JLabel jLabel = new JLabel(text);
        jLabel.setBounds(0,0,100,80);
        jLabel.setFont(new Font("Consolas", Font.BOLD, 36));
        jDialog.setBounds(0, 0, 100, 80);
        jDialog.setTitle("Error");
        jDialog.getContentPane().add(jLabel);
        jDialog.setLocationRelativeTo(null);
        jDialog.setModal(true);
        jDialog.setVisible(true);
    }
    //方法接受两个参数，一个是文件路径，一个是待比较的文本
    public static boolean compare(String filePath, String text) {
        try {
            // 打开文件并使用BufferedReader读取文件
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String line = reader.readLine(); // 逐行读取文件内容
            while (line != null) {
                //找到了则返回ture
                if (line.equals(text)) {
                    reader.close();
                    return true;
                }
                line = reader.readLine();
            }
            reader.close();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false; // 出现异常时返回false
        }
    }
}
